package com.edgclub.edg.utils.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.edgclub.edg.model.User;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.model.UserAuth;

import java.util.Date;

/**
 * 用户信息存储
 * 
 * @author 陈坤彬
 *
 */
public class UserPrefUtils {

	private static final String USERPRENAME = "gz_user_pref_3.0";

	private Context mContext;

	private SharedPreferences userPref;

	private Editor userEditor;

	private boolean isLogin = false;

	public UserPrefUtils(Context mContext) {
		this.mContext = mContext;
		userPref = mContext.getSharedPreferences(USERPRENAME,
				Context.MODE_PRIVATE);
		userEditor = userPref.edit();
	}

	/**
	 * 保存用户,传空的时候相当于对用户信息进行清除
	 * 
	 * @param user
	 */
	public void saveUser(User user) {
		// 空的时候相当于清除操作
		if (null == user) {
			boolean isGuide = userPref.getBoolean("guide", true);
			userEditor.clear();
			userEditor.putBoolean("guide", isGuide);
			userEditor.commit();
		} else {
			userEditor.putString("id", user.getId()+"");
			userEditor.putString("gzno", user.getGzno());
			userEditor.putInt("province", user.getProvince());
			userEditor.putInt("city", user.getCity());
			userEditor.putString("cityName", user.getCityName());
			userEditor.putInt("sex", user.getSex());
			userEditor.putInt("age", user.getAge());
			userEditor.putString("birth", user.getBirth());
			userEditor.putString("nick", user.getNick());
			userEditor.putString("phone", user.getPhone());
			userEditor.putString("imid", user.getImid());
			userEditor.putInt("district", user.getDistrict());
			userEditor.putString("school", user.getSchool());
			userEditor.putString("company", user.getCompany());
			userEditor.putString("logo",
					user.getLogo() != null ? user.getLogo() : "");
			userEditor.putString("hdLogo",
					user.getHdLogo() != null ? user.getHdLogo() : "");
			userEditor.putString("hobby", user.getHobby());
			userEditor.putString("job", user.getJob());
			userEditor.putString("intro", user.getIntro());
			userEditor.putString("msgType", user.getMsgType());
			userEditor.putString("enable", user.getEnable());
			userEditor.putInt("roleCode",user.getRoleCode());
			UserAuth userAuth = user.getUserAuth();
			if (userAuth != null){
				userEditor.putString("type", user.getUserAuth().getType());
			}
			if (isLogin){
				userEditor.putString("token", user.getToken());
			}

		}

		userEditor.commit();
		EMClient.getInstance().updateCurrentUserNick(user.getNick());
	}
	public void savetoken(String token) {
		userEditor.putString("token", token);
		userEditor.commit();
	}
	/**
	 * 获取用户
	 * 
	 * @return
	 */
	public User getUser() {
		User user = new User();
		user.setRoleCode(userPref.getInt("roleCode",0));
		user.setId(userPref.getString("id","")+"");
		user.setGzno(userPref.getString("gzno", ""));
		user.setNick(userPref.getString("nick", ""));
		user.setPhone(userPref.getString("phone", userPref.getString("name", "")));
		user.setImid(userPref.getString("imid", ""));
		user.setProvince(userPref.getInt("province", 0));
		user.setCity(userPref.getInt("city", 0));
		user.setCityName(userPref.getString("cityName", ""));
		user.setBirth(userPref.getString("birth", ""));
		user.setAge(userPref.getInt("age", 0));
		user.setDistrict(userPref.getInt("district", 0));
		user.setSchool(userPref.getString("school", ""));
		user.setCompany(userPref.getString("company", ""));
		user.setHobby(userPref.getString("hobby", ""));
		user.setJob(userPref.getString("job", ""));
		user.setSex(userPref.getInt("sex", -1));
		user.setLogo(userPref.getString("logo", ""));
		user.setHdLogo(userPref.getString("logo", ""));
		user.setIntro(userPref.getString("intro", ""));
		user.setMsgType(userPref.getString("msgType", ""));
		user.setEnable(userPref.getString("enable", ""));
		user.setToken(userPref.getString("token", ""));
		UserAuth userAuth = new UserAuth();
		userAuth.setType(userPref.getString("type", "-1"));
		user.setUserAuth(userAuth);
		return user;
	}
	
	public String getGzno() {
		return userPref.getString("gzno", "");
	}

	public String getPSW() {
		return userPref.getString("psw", "");
	}

	public void savePSW(String psw) {
		userEditor.putString("psw", psw);
		userEditor.commit();
	}
	public String getName() {
		return userPref.getString("name", userPref.getString("phone", ""));
	}

	public void saveName(String name) {
		userEditor.putString("name", name);
		userEditor.commit();
	}
	/**
	 * 是否加薪
	 * 
	 * @return
	 */
	public boolean isRaiseTopic() {
		return userPref.getBoolean("raiseTopic", false);
	}

	/**
	 * 存储加薪
	 * 
	 * @param raiseTopic
	 */
	public void setRaiseTopic(boolean raiseTopic) {
		userEditor.putBoolean("raiseTopic", raiseTopic);
		userEditor.commit();
	}
	/**
	 * 是否删帖
	 *
	 * @return
	 */
	public boolean isDeletTopic() {
		return userPref.getBoolean("deletTopic", false);
	}

	/**
	 * 存储删帖
	 *
	 * @param deletTopic
	 */
	public void setDeletTopic(boolean deletTopic) {
		userEditor.putBoolean("deletTopic", deletTopic);
		userEditor.commit();
	}
	/**
	 * 是否删回复
	 *
	 * @return
	 */
	public boolean isDeletReply() {
		return userPref.getBoolean("deletReply", false);
	}

	/**
	 * 存储删回复
	 *
	 * @param deletReply
	 */
	public void setDeletReply(boolean deletReply) {
		userEditor.putBoolean("deletReply", deletReply);
		userEditor.commit();
	}
	/**
	 * 是否删回复
	 *
	 * @return
	 */
	public boolean isMoveTopic() {
		return userPref.getBoolean("moveTopic", false);
	}

	/**
	 * 存储删回复
	 *
	 * @param moveTopic
	 */
	public void setMoveTopic(boolean moveTopic) {
		userEditor.putBoolean("moveTopic", moveTopic);
		userEditor.commit();
	}
	/**
	 * 是否有上传图片
	 * 
	 * @return
	 */
	public boolean isLogo() {
		return !userPref.getString("logo", "").equals("");
	}


	/**
	 * 保存第三方openId和登录类型 0qq,1微信，2微博
	 * 
	 * @param openId
	 * @param types
	 */
	public void saveThirdLogin(String openId, String types) {
		userEditor.putString("openid", openId);
		userEditor.putString("types", types);
		userEditor.commit();
	}

	public String getOpenId() {
		return userPref.getString("openid", "");
	}

	public String getLoginType() {
		return userPref.getString("types", "");
	}




	/**
	 * 保存提示的状态
	 */
	public void saveLoginTime(long loginTime) {
		userEditor.putLong("logintime", loginTime);
		userEditor.commit();
	}
	public void clearUser() {
		userEditor.clear();
		userEditor.commit();
	}

	/**
	 * 是否需要进行提示
	 *
	 * @return
	 */
	public long getLoginTime() {
		return userPref.getLong("logintime", new Date().getTime());
	}

	public boolean isLogin() {
		return isLogin;
	}

	public void setIsLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	/**
	 * 消息数量
	 * @param num
     */
	public void setMessNum(int num){
		userEditor.putInt("messNum", num);
		userEditor.commit();
	}
	public int getMessNum(){
		return userPref.getInt("messNum", 0);
	}

	/**
	 * 举报列表数量
	 * @param num
	 */
	public void setReportNum(int num){
		userEditor.putInt("reportNum", num);
		userEditor.commit();
	}
	public int getReportNum(){
		return userPref.getInt("reportNum", 0);
	}

	/**
	 * 保存举报提示的状态
	 */
	public void saveReportTip(boolean res) {
		userEditor.putBoolean("reporttip", res);
		userEditor.commit();
	}

	/**
	 * 是否需要进行举报提示
	 *
	 * @return
	 */
	public boolean getReportTip() {
		return userPref.getBoolean("reporttip", false);

	}

	/**
	 * 保存提示的状态
	 */
	public void saveMessTip(boolean res) {
		userEditor.putBoolean("messtip", res);
		userEditor.commit();
	}

	/**
	 * 是否需要进行提示
	 *
	 * @return
	 */
	public boolean getMessTip() {
		return userPref.getBoolean("messtip", false);

	}
	/**
	 * 保存消息的状态
	 */
	public void saveMesssetting(boolean res) {
		userEditor.putBoolean("messsetting", res);
		userEditor.commit();
	}

	/**
	 * 是否需要进行提示
	 *
	 * @return
	 */
	public boolean getMesssetting() {
		return userPref.getBoolean("messsetting", true);

	}

	/**
	 * 保存消息的状态
	 */
	public void saveAdminMesssetting(boolean res) {
		userEditor.putBoolean("adminmesssetting", res);
		userEditor.commit();
	}

	/**
	 * 是否需要进行提示
	 *
	 * @return
	 */
	public boolean getAdminMesssetting() {
		return userPref.getBoolean("adminmesssetting", true);

	}

	/**
	 * 是否能审批
	 *
	 * @return
	 */
	public boolean isReportTopic() {
		return userPref.getBoolean("reportTopic", false);
	}

	/**
	 * 获取是否能审批
	 *
	 * @param reportTopic
	 */
	public void setReportTopic(boolean reportTopic) {
		userEditor.putBoolean("reportTopic", reportTopic);
		userEditor.commit();
	}

	/**
	 * 是否能禁止用户和封设备号
	 *
	 * @return
	 */
	public boolean isBanDevice() {
		return userPref.getBoolean("banDevice", false);
	}

	/**
	 * 获取是否能禁止用户和封设备号
	 *
	 * @param banDevice
	 */
	public void setBanDevice(boolean banDevice) {
		userEditor.putBoolean("banDevice", banDevice);
		userEditor.commit();
	}

	/**
	 * 是否能管理群组
	 *
	 * @return
	 */
	public boolean isGroupAdmin() {
		return userPref.getBoolean("groupAdmin", false);
	}

	/**
	 * 获取是否能管理群组
	 *
	 * @param groupAdmin
	 */
	public void setGroupAdmin(boolean groupAdmin) {
		userEditor.putBoolean("groupAdmin", groupAdmin);
		userEditor.commit();
	}
}
